<template>
  <div>
    <div class="update-birthdy">
      <van-datetime-picker
        v-model="currentDate"
        type="date"
        title="选择年月日"
        :min-date="minDate"
        :max-date="maxDate"
        @confirm="onConfirm"
        @cancel="$emit('close')"
        @change="onChange"
      />
    </div>
  </div>
</template>

<script>
import { updateUserProfile } from '@/api/user'
import * as dayjs from 'dayjs'
export default {
  data () {
    return {
      minDate: new Date(1900, 0, 1),
      maxDate: new Date(),
      currentDate: new Date(),
      birthday: ''
    }
  },
  methods: {
    onChange (picker, value, index) {
      this.birthday = this.currentDate
    },
    async onConfirm () {
      this.$toast.loading({
        message: '保存中....',
        forbidClick: true,
        duration: 0
      })
      try {
        await updateUserProfile({
          birthday: dayjs(this.birthday).format('YYYY-MM-DD')
        })
        this.$toast('修改成功')
      } catch (error) {
        this.$toast('修改失败')
      }
      this.$emit('close')
    }
  }
}
</script>

<style>
</style>
